package com.yonyou.pmclouds.basecom.impl;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.yonyou.pmclouds.basecom.exception.BusinessException;
import com.yonyou.pmclouds.basecom.runtime.RuntimeEnvironment;
import com.yonyou.pmclouds.ncinvoker.entity.NCInvokerResult;
import com.yonyou.pmclouds.ncinvoker.entity.NCServiceVO;
import com.yonyou.pmclouds.ncinvoker.itf.NCInvoker;
import com.yonyou.pmclouds.plan.plandriven.entity.NccMessageParamVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;

/**
 * 向ncc发送待办消息服务类
 *
 * @author th
 * @date 2019/11/20 18:32
 */
@Component
@Slf4j
public class NCCSendMessageService {
    public static final String SEND_MESSAGE_SERVICE_NAME = "PushMesForPMCScheServlet";
    public static final String NCC_SERVICE_SERVLET_PATH = "/service/PushMesForPMCScheServlet";
    public static final String NC_UBL_SERVICE = "nc.bs.pmcloud.servlet.ublimpl.PushMesImp4UBL";

    @Autowired
    private NCInvoker invoker;

    private ObjectMapper mapper = new ObjectMapper();

    /**
     * 计划完成，给后置计划发送消息通知
     *
     * @param paramVO
     * @return
     * @throws BusinessException
     */
    public boolean sendMessageToNCC(NccMessageParamVO paramVO) throws BusinessException {
        if (paramVO == null)
            return false;

        // 调用NCC发送待办消息接口
        Map<String, Object> requestParams = new HashMap<>();
        requestParams.put("pkOrg", paramVO.getPkOrg());
        requestParams.put("pkProject", paramVO.getPkProject());
        requestParams.put("infos", paramVO.getInfos());

        // 返回结果
        NCInvokerResult invokeResult = null;
        Map resultMap;

        try {

            NCServiceVO serviceVO = new NCServiceVO(SEND_MESSAGE_SERVICE_NAME,NCC_SERVICE_SERVLET_PATH,NC_UBL_SERVICE);
            // 调NC接口
            invokeResult = invoker.invokeSync(serviceVO, requestParams, RuntimeEnvironment.getTenantId(),
                    RuntimeEnvironment.getLoginUserId());
            if (invokeResult == null || invokeResult.getResult() == null) {
                log.error("计划待办消息发送失败, 返回结果: {}", invokeResult == null ? null : invokeResult.getResult());
                return false;
            }

            if (!NCInvokerResult.STATUS_OK.equals(invokeResult.getStatus())) {
                log.error("计划待办消息发送失败, 返回状态: {}, 返回结果: {}", invokeResult.getStatus(), invokeResult.getResult());
                return false;
            }

            resultMap = mapper.readValue(invokeResult.getResult(), Map.class);
            if (!"ok".equals(resultMap.get("resultCode"))) {
                log.error("计划待办消息发送失败，返回result：{}", invokeResult.getResult());
                return false;
            }
        } catch (IOException e) {
            log.error("unserialization failed, result is {}", invokeResult.getResult());
            log.error("计划待办消息发送结果反序列失败", e);
            return false;
        } catch (Exception e) {
            log.error("计划待办消息发送失败, 返回结果: {}", invokeResult == null ? null : invokeResult.getResult());
            log.error("计划待办消息发送失败", e);
            return false;
        }

        return true;
    }

}
